import { query } from './mySQLdb';


// 创建用户
export async function createUserData(username: string, email: string, password: string) {
    const sql = 'INSERT INTO users_data (username, email, password) VALUES (?, ?, ?)';
    const params = [username, email, password];
    try {
        const result = await query(sql, params);
        console.log(result, "插入成功");
        return { id: result.insertId, username, email, password };
    } catch (error) {
        throw error;
    }
}
// 获取所有用户
export async function getAllUsers() {
    const sql = 'SELECT * FROM users_data';
    try {
        const users = await query(sql);
        return users;
    } catch (error) {
        throw error;
    }
}

// 根据 ID 获取用户
export async function getUserById(id) {
    const sql = 'SELECT * FROM users WHERE id = ?';
    const params = [id];
    try {
        const [user] = await query(sql, params);
        return user;
    } catch (error) {
        throw error;
    }
}

// 根据 参数 获取用户
export async function searchDatas(passQql: string, data: []) {
    const sql = passQql;
    const params = data;
    try {
        const result = await query(sql, params);
        return result;
    } catch (error) {
        throw error;
    }
}

// 删除用户
export async function deleteUser(id) {
    const sql = 'DELETE FROM users WHERE id = ?';
    const params = [id];
    try {
        await query(sql, params);
        return { id };
    } catch (error) {
        throw error;
    }
}